#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/16z9oj/012113_challenge_118_easy_date_localization/

require 'time'

# 1.8.7 doesn't have millisecond granularity in sprintf :(
mil = sprintf("%03d", `date +%N`.strip.to_i / (1000 * 1000))
now = Time.now

STDIN.readlines.each do |fmt|
    # Some target characters are the same as the input format characters.
    #
    # Example is (%m => %M) and (%M => %m).
    #
    # To avoid having to specify an intermediate placeholder and for consistency,
    # just translate directly to the desired time element for everything
    fmt = fmt.strip.
        gsub("%l", mil). # millis
        gsub("%s", now.strftime("%S")). # seconds
        gsub("%m", now.strftime("%M")). # minutes
        gsub("%h", now.strftime("%I")). # hours (1-12)
        gsub("%H", now.strftime("%H")). # hours (0-23)
        gsub("%c", now.strftime("%p")). # AM/PM
        gsub("%d", now.strftime("%d")). # day (1-31)
        gsub("%M", now.strftime("%m")). # month (1-12)
        gsub("%y", now.strftime("%Y"))  # year (4 digit)

    puts fmt
end
